package com.shenjinzheng.sso.controller;


import com.shenjinzheng.common.Result;
import com.shenjinzheng.member.entity.MemberInfo;
import com.shenjinzheng.sso.dto.MemberLoginDTO;
import com.shenjinzheng.sso.dto.SendSmsDTO;
import com.shenjinzheng.sso.service.MemberAuthService;
import com.shenjinzheng.sso.service.MemberService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author shenjinzheng
 * @since 2024-10-27
 */
@Slf4j
@RestController
@RequestMapping("/member")
public class MemberController {

    @Resource
    private MemberService memberService;

    @Resource
    private Map<String, MemberAuthService> memberAuthServiceHashMap;

    @PostMapping("/login")
    public Result<String> login(@RequestBody MemberLoginDTO memberLoginDTO) {
        log.info("MemberLoginDTO:{}", memberLoginDTO);
        MemberAuthService memberAuthService = memberAuthServiceHashMap.get(memberLoginDTO.getType());
        String token = memberAuthService.login(memberLoginDTO);
        return Result.success(token);
    }

    @PostMapping("/getCode")
    public Result<String> getCode(@RequestBody SendSmsDTO sendSmsDTO) {
        //memberServiceHashMap.forEach((k, v) -> System.out.println(k + ":" + v));
        MemberAuthService memberAuthService = memberAuthServiceHashMap.get("memberServiceImplByCode");
        String code = memberAuthService.getCode(sendSmsDTO.getPhone());
        return Result.success(code);
    }

    @GetMapping("findByToken")
    public Result<MemberInfo> findByToken(){
        MemberInfo memberInfo =memberService.findByToken();
        return Result.success(memberInfo);
    }

    @GetMapping("test")
    public Result<Object> test(){
        return Result.success();
    }

}
